import 'package:flutter/material.dart';

/*
 * 进度指示器 菊花
 * name: LoadingUtil
 * date: 2021-03-09 16:29:11
 * autho: zwb
 * desc 在要使用菊花的地方使用此组件即可
 * 例：
 * body: LoadProgress(
 *    loading: _loading,
 *    child: 需要转圈圈效果的地方
 * )
 */
class LoadingUtil extends StatefulWidget {
  final Widget child; // 内容主体
  late final bool loading; // 是否显示转圈圈
  final Widget? load;  // 菊花
  final Widget? text; // 文本
  final Widget? widget; // 自定义
  LoadingUtil({this.child:const SizedBox(),this.loading:false,this.text,this.load, this.widget});
  @override
  _LoadingUtilState createState() => _LoadingUtilState();
}

class _LoadingUtilState extends State<LoadingUtil> {

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // 内容主体
        widget.child,

        // 进度浮层
        Offstage(
          offstage: !widget.loading,
          child: WillPopScope(
            onWillPop: () async{ // 返回时 关闭菊花
              setState(() => widget.loading = false);
              return true;
            },
            child: Container(
              alignment: Alignment.center,
              color: Colors.black26,
              child: Material(
                color: Colors.transparent,
                child:  ClipRRect(
                  child: Container(
                    width: 100,
                    height: 100,
                    color: Colors.white,
                    padding: EdgeInsets.all(20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        widget.load ?? Padding(padding: EdgeInsets.only(bottom: widget.text != null ? 5:0 ),child: CircularProgressIndicator(strokeWidth: 5,),),
                        widget.text ?? SizedBox(),
                      ],
                    ),
                ), borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
